package com.toman.ui.work.workshop;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Adpter.ItemListAdapter;
import com.toman.Enum.BaseEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.DepartmentEntity;
import com.toman.JavaBean.OperationArtlineEntity;
import com.toman.JavaBean.PlanEntity;
import com.toman.JavaBean.ProductEntity;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.JsonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.Util.BaseNetUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class PlanAddActivity extends ToolBarActivity {

    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.rl_product_dep)
    RelativeLayout rlProductDep;
    @BindView(R.id.et_order_plan_code)
    EditText etOrderPlanCode;
    @BindView(R.id.rl_order_code)
    RelativeLayout rlOrderCode;
    @BindView(R.id.et_order_num)
    EditText etOrderNum;
    @BindView(R.id.rl_order_num)
    RelativeLayout rlOrderNum;
    @BindView(R.id.et_order_code)
    EditText etOrderCode;
    @BindView(R.id.et_customer)
    EditText etCustomer;
    @BindView(R.id.et_order_need)
    EditText etOrderNeed;
    @BindView(R.id.tv_order_lastday)
    TextView tvOrderLastday;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_beltline)
    TextView tvBeltline;
    @BindView(R.id.rl_product_beltline)
    RelativeLayout rlProductBeltline;
    @BindView(R.id.tv_order_remark_name)
    TextView tvOrderRemarkName;
    @BindView(R.id.et_order_remark)
    EditText etOrderRemark;
    @BindView(R.id.btn_ok)
    Button btnOk;
    PlanEntity bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_add);
        ButterKnife.bind(this);
        initToolBar("新增" + BaseEnum.PLAN.name);
        initView();
    }

    private void initView() {
        bean=new PlanEntity();
        bean.setDepartmentEntity(new DepartmentEntity());
        bean.setOperationArtlineEntity(new OperationArtlineEntity());
        bean.setProductEntity(new ProductEntity());
//        ViewUtil.edit(etCustomer,etOrderCode,etOrderNeed,etOrderNum,etOrderPlanCode,etOrderRemark);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_finish, menu);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_finish:
                        add();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    public void add() {
        String[] item = new String[]{"部门",  "最后交付日", "净需求数", "品名", "工艺路线名称"};//直接初始化
        if (ViewUtil.isNull(getApplicationContext(), item, tvDep, tvOrderLastday, etOrderNeed, tvProductName, tvBeltline)) {
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("departmentId", bean.getDepartmentEntity().getId() + "");
        map.put("customer", etCustomer.getText().toString());
        map.put("orderNum", etOrderCode.getText().toString());
        map.put("provideDate", tvOrderLastday.getText().toString());
        map.put("demandCount", etOrderNeed.getText().toString());
        map.put("productId", bean.getProductEntity().getId() + "");
        map.put("artlineId", bean.getOperationArtlineEntity().getId() + "");
        map.put("remark", etOrderRemark.getText().toString());

        String str = JsonUtils.obj2JsonString(map);
        OkHttpUtils
                .post(NewBaseUrl.workshop_plan_save)
                .headers("token", token)
                .headers("Content-Type", "application/json")
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "新增计划: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });

    }
    public void dilogArtLine() {
        ItemListAdapter adapter=new ItemListAdapter(this);
        List<ItemListBean> list = new ArrayList<>();
        final AlertDialog dialog;
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.dialog_list, null);
        ListView lv = (ListView) textEntryView.findViewById(R.id.lv);
        lv.setAdapter(adapter);
        builder                //对话框的标题
                .setTitle("选择工艺线路")
                //设定显示的View
                .setView(textEntryView)
                //对话框中的“登陆”按钮的点击事件
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        //获取用户输入的“用户名”，“密码”
                    }
                })
                //对话框的“退出”单击事件
//                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                    }
//                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                .create();
        dialog = builder.show();
        BaseNetUtil.artline(this, list, token, mCompanyId, adapter, bean.getProductEntity().getProductName());

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemListBean bean1 = adapter.getItem(i);
                bean.getOperationArtlineEntity().setArtlineName(bean1.getName());
                bean.getOperationArtlineEntity().setId(Long.valueOf(bean1.getID()));
                tvBeltline.setText(bean.getOperationArtlineEntity().getArtlineName());

                dialog.dismiss();
            }
        });
    }
    public void dilogDep() {
        DialogUtils.showDialog(this, "部门", NewBaseUrl.department_list_get, "departmentName", new DefaultAcitonCallback() {
            @Override
            public void run(ItemListBean bean1, Dialog dialog) {
                bean.getDepartmentEntity().setDepartmentName(bean1.getName());
                bean.getDepartmentEntity().setId(Long.valueOf(bean1.getID()));
                tvDep.setText(bean.getDepartmentEntity().getDepartmentName());
                dialog.dismiss();
            }
        });
    }
// public void dilogDep() {
//        ItemListAdapter adapter=new ItemListAdapter(this);
//        List<ItemListBean> list = new ArrayList<>();
//        final AlertDialog dialog;
//        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        LayoutInflater factory = LayoutInflater.from(this);
//        final View textEntryView = factory.inflate(R.layout.dialog_list, null);
//        ListView lv = (ListView) textEntryView.findViewById(R.id.lv);
//        lv.setAdapter(adapter);
//        builder                //对话框的标题
//                .setTitle("选择部门")
//                //设定显示的View
//                .setView(textEntryView)
//                //对话框中的“登陆”按钮的点击事件
//                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//
//                        //获取用户输入的“用户名”，“密码”
//                    }
//                })
//                //对话框的“退出”单击事件
////                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
////                    public void onClick(DialogInterface dialog, int whichButton) {
////                    }
////                })
//                // 设置dialog是否为模态，false表示模态，true表示非模态
//                .setCancelable(true)
//                .create();
//        dialog = builder.show();
//        BaseNetUtil.dep(this, list, token, mCompanyId, adapter);
//        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                ItemListBean bean1 = adapter.getItem(i);
//                bean.getDepartmentEntity().setDepartmentName(bean1.getName());
//                bean.getDepartmentEntity().setId(Long.valueOf(bean1.getID()));
//                tvDep.setText(bean.getDepartmentEntity().getDepartmentName());
//                dialog.dismiss();
//            }
//        });
//    }

    public void dilogProduct() {
        List<ProductEntity> list_product = new ArrayList<>();
        ItemListAdapter adapter=new ItemListAdapter(this);
        List<ItemListBean> list = new ArrayList<>();
        final AlertDialog dialog;
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.dialog_list, null);
        ListView lv = (ListView) textEntryView.findViewById(R.id.lv);
        lv.setAdapter(adapter);
        builder                //对话框的标题
                .setTitle("选择品名")
                //设定显示的View
                .setView(textEntryView)
                //对话框中的“登陆”按钮的点击事件
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        //获取用户输入的“用户名”，“密码”
                    }
                })
                //对话框的“退出”单击事件
//                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton) {
//                    }
//                })
                // 设置dialog是否为模态，false表示模态，true表示非模态
                .setCancelable(true)
                .create();
        dialog = builder.show();
        list_product = BaseNetUtil.product(this, list, token, mCompanyId, adapter);

        List<ProductEntity> finalList_product = list_product;
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ItemListBean bean1 = adapter.getItem(i);
                bean.setProductEntity(finalList_product.get(i));
                bean.getOperationArtlineEntity().setId(bean.getProductEntity().getArtlineEntity().getId());
                tvProductName.setText(bean.getProductEntity().getProductName());
                tvBeltline.setText(bean.getProductEntity().getArtlineEntity().getArtlineName());
                dialog.dismiss();
            }
        });
    }
    @OnClick({R.id.tv_dep, R.id.tv_order_lastday, R.id.tv_product_name, R.id.tv_beltline, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_dep:
                dilogDep();
                break;
            case R.id.tv_order_lastday:
                ViewUtil.getDate(this, tvOrderLastday);
                break;
            case R.id.tv_product_name:
                dilogProduct();
                break;
            case R.id.tv_beltline:
                dilogArtLine();
                break;

        }
    }
}
